//
//  InputMessageCell.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/1/3.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "InputMessageCell.h"
#import "SelectCountryVC.h"
#import <IQKeyboardManager.h>

@interface InputMessageCell ()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *chooseOtherBtn;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *inputTF;

@end

@implementation InputMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.inputTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

+ (instancetype)getCellWithTableView:(UITableView *)tableView index:(NSInteger)index {
    
    if (index < 0 || index > 2) {
        [IGUtil showToast:MYLocalizedString(@"PUBLIC_SYSTEM_ERROR", nil)];
        
        return nil;
    }
    
    NSString *identifier = [NSString stringWithFormat:@"cell%ld",(index + 1)];
    InputMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"InputMessageCell" owner:self options:nil] objectAtIndex:index];
    }
    
    return cell;
}

- (void)configWithDict:(NSDictionary *)dict {
    
    self.nameLabel.text = dict[@"name"];
    [self.chooseOtherBtn setTitle:dict[@"choose"] forState:UIControlStateNormal];
    self.inputTF.placeholder = dict[@"inputPH"];
    self.inputTF.text = dict[@"namevalue"];
    [self.getCodeBtn setTitle:dict[@"getCode"] forState:UIControlStateNormal];
}

- (void)setType:(NSInteger)type {
    
    _type = type;
    
    if (type == 4) {
        self.inputTF.userInteractionEnabled = NO;
    } else {
        self.inputTF.userInteractionEnabled = YES;
    }
    
    if (type == 1 || type == 3) {
        
        self.inputTF.secureTextEntry = YES;
    }
}
- (IBAction)getCodeAction:(id)sender {
    
    [_delegate getCodeAction:sender];
}

- (IBAction)chooseCountryAction:(UIButton *)sender {
    
    SelectCountryVC *countryVC = [[SelectCountryVC alloc] init];
    countryVC.delegate = self;
    [countryVC showWithVC:self.superVC];
}

#pragma mark -SelectCountryVCDelegate
- (void)selectedCountry:(Country *)country {
    
    [self.chooseOtherBtn setTitle:country.code forState:UIControlStateNormal];
//    [self.chooseOtherBtn setTitleColor:kContentColor forState:UIControlStateNormal];
//    
//    [self.chooseOtherBtn setImage:[UIImage imageNamed:@"下拉"] forState:UIControlStateNormal];
//    
//    CGFloat imageWith = CGRectGetWidth(self.chooseOtherBtn.imageView.frame);
//    
//    CGFloat labelWidth = [IGUtil getSizeContent:country.code width:MAXFLOAT font:FontBold(14)].width;
//    
//    self.chooseOtherBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
//    self.chooseOtherBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWith, 0, imageWith);
    
    [_delegate selectedCountry:country type:self.type];
}

- (void)textFieldDidChange:(UITextField *)sender {
    
    [_delegate inputMsg:sender.text type:self.type];
}

@end
